package com.example.mapp;

import com.example.entity.authority.Party;
import com.example.entity.paperlessMaintance.PaperlessMaintanceApply;
import com.example.ext.entity.ExtElevator;
import com.example.mapp.buildSql.PaperlessMaintMappSql;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface PaperlessMaintMapp {

    @SelectProvider(type = PaperlessMaintMappSql.class,method = "queryPaperlessMaint")
    List<PaperlessMaintanceApply> query(PaperlessMaintanceApply paperlessMaintanceApply) throws Exception;

    @UpdateProvider(type = PaperlessMaintMappSql.class,method = "updatePaperlessMaint")
    int update(PaperlessMaintanceApply paperlessMaintanceApply) throws Exception;

    @Insert("INSERT INTO paperless_maintance_apply (apply_date, apply_status, create_company_name, elevator_ids, " +
            "letter_of_commitment_doc, maintenance_party_id, maintenance_party_name, other_notes, parent_parent_party_id," +
            " jg_party_id,jg_party_name,remarks, suspend_reason, suspend_remarks, user_party_id, user_party_name) " +
            "VALUES (#{applyDate}, #{applyStatus}, #{createCompanyName},#{elevatorIds}, #{letterOfCommitmentDoc}," +
            " #{maintenancePartyId},#{maintenancePartyName},#{otherNotes},#{parentParentPartyId},#{jgPartyId},#{jgPartyName}," +
            " #{remarks},#{suspendReason},#{suspendRemarks},#{userPartyId},#{userPartyName});\n")
    @SelectKey(statement = "select last_insert_id()", keyProperty = "id", keyColumn = "id",before = false,resultType = Long.class)
    int insert(PaperlessMaintanceApply paperlessMaintanceApply) throws Exception;

    @Select("select * from paperless_maintance_apply where id = #{id}")
    PaperlessMaintanceApply queryById(String id) throws Exception;

    @Delete("delete from paperless_maintance_apply where id = #{id}")
    int delete(String id) throws Exception;

    @SelectProvider(type = PaperlessMaintMappSql.class,method = "getOptionalJgParty")
    List<Party> getOptionalParty(ExtElevator elevator) throws Exception;

    @Select("SELECT" +
            "  p.party_name AS 'partyName'," +
            "  p.id         AS 'id' " +
            "FROM party p " +
            "WHERE p.id IN ( " +
            "  SELECT DISTINCT py.parent_id " +
            "  FROM party py " +
            "  WHERE py.id IN (SELECT DISTINCT er.branch_office " +
            "                  FROM elevator er " +
            "                  WHERE er.maintenance_party_id = #{maintenancePartyId} AND er.branch_office IS NOT NULL) " +
            "        AND py.parent_id IS NOT NULL) ")
    List<Map<String,Object>> paperlessGetApplicantList(Map map);


    @SelectProvider(type = PaperlessMaintMappSql.class,method = "paperlessGetAreaElevatorList")
    List<Map<String,Object>> paperlessGetAreaElevatorList(Map map);

    @Select("select tot.elevator_num as 'elevatorNum' from ( " +
            "  SELECT er.elevator_num " +
            "  FROM elevator er " +
            "  WHERE er.branch_office IN (SELECT id  " +
            "                             FROM party " +
            "                             WHERE parent_id = #{jgPartyId}) " +
            "  AND er.maintenance_party_id = #{maintenancePartyId}) tot " +
            "  INNER JOIN ( " +
            "   select elevator_num,count(1) as num from ( " +
            "   SELECT  elevator_num FROM traction_half_mon thm where thm.maintenance_party_id=#{maintenancePartyId} " +
            "   UNION ALL " +
            "   SELECT  elevator_num FROM debris_half_mon where maintenance_party_id=#{maintenancePartyId} " +
            "   UNION ALL " +
            "   SELECT  elevator_num FROM escalator_half_mon where maintenance_party_id=#{maintenancePartyId}" +
            "   UNION ALL " +
            "   SELECT  elevator_num FROM hydpress_half_mon where maintenance_party_id=#{maintenancePartyId}) par GROUP BY par.elevator_num " +
            "  HAVING num>=2 " +
            " ) top ON tot.elevator_num = top.elevator_num ")
    List<String> paperlessGetAreaTwoElevator(Map map);

    @Insert("INSERT INTO paperless_maintance_apply (apply_date, apply_status, create_company_name, elevator_ids, " +
            "letter_of_commitment_doc, maintenance_party_id, maintenance_party_name, other_notes, parent_parent_party_id," +
            " jg_party_id,jg_party_name,remarks, suspend_reason, suspend_remarks, user_party_id, user_party_name) " +
            "VALUES (#{applyDate}, #{applyStatus}, #{createCompanyName},#{elevatorIds}, #{letterOfCommitmentDoc}," +
            " #{maintenancePartyId},#{maintenancePartyName},#{otherNotes},#{parentParentPartyId},#{jgPartyId},#{jgPartyName}," +
            " #{remarks},#{suspendReason},#{suspendRemarks},#{userPartyId},#{userPartyName}) ")
    void insertList(Map map);


    @Select("SELECT" +
            "  pma.maintenance_party_name AS 'maintenancePartyName', " +
            "  p.party_addr               AS 'partyAddr', " +
            "  p.credit_code              AS 'creditCode', " +
            "  p.corporation              AS 'corporation', " +
            "  p.telephone                AS 'telephone', " +
            "  p.zzdj                     AS 'zzdj', " +
            "  p.maint_start              AS 'maintStart', " +
            "  p1.party_name          AS 'jgPartyName' " +
            "  FROM paperless_maintance_apply pma " +
            "  LEFT JOIN party p ON p.id = pma.maintenance_party_id "+
            "  LEFT JOIN party p1 ON p1.id = pma.jg_party_id "+
            "  where pma.id = #{id} ")
    Map<String,String> getPaperLessInfo(String id);

    @Update(" update paperless_maintance_apply set apply_status = #{applyStatus},remarks = #{remarks} where id = #{id}  ")
    void updateCheckType(PaperlessMaintanceApply paperlessMaintanceApply);

    @Update(" update elevator set is_paperless_maintenance = #{isPaperlessMaintenance} where id = #{id}  ")
    void updateIsPaperless(Map<String, Object> map);
}
